package com.space.controller.no;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.space.base.ApiResponse;
import com.space.base.ErrorCode;
import com.space.config.ConfigUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sqllite/api")
@Slf4j
@Api("配置接口")
public class ConfigController {

//    static {   ConfigUtil.createNewTable();}

    @PostMapping("/add")
    @ApiOperation("buildFile")
    public ApiResponse<String> buildFile(@RequestBody String v){
        try {
            JSONObject obj= JSON.parseObject(v, JSONObject.class);
            if("erro".equals( ConfigUtil.selectById(1)))  ConfigUtil.createNewTable();

            ConfigUtil.insert(obj.getString("data"),obj.getIntValue("id"));

            ApiResponse<String> result = new ApiResponse();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }


    }

    @PostMapping("/update")
    @ApiOperation("update")
    public ApiResponse<String> update(@RequestBody String v){
        try {
            JSONObject obj= JSON.parseObject(v, JSONObject.class);
            if("erro".equals( ConfigUtil.selectById(1)))  ConfigUtil.createNewTable();

            ConfigUtil.update(obj.getIntValue("id"),obj.getString("data"));

            ApiResponse<String> result = new ApiResponse();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }


    }


    @PostMapping("/removeByIds")
    @ApiOperation("deleteFile")
    public ApiResponse<String> deleteFile(@RequestBody String v){
        try {
            JSONObject obj= JSON.parseObject(v, JSONObject.class);

            JSONArray ids= obj.getJSONArray("ids");
            Iterator<Object> i = ids.iterator();
            while (i.hasNext()){
                Object j=  i.next();
                ConfigUtil.delete((Integer) j);
            }

            ApiResponse<String> result = new ApiResponse();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }
    }



    @GetMapping("/removeAll")
    @ApiOperation("deleteAllFile")
    public ApiResponse<String> deleteAllFile(){
        try {

            ConfigUtil.drop();

            ApiResponse<String> result = new ApiResponse();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }
    }

    @PostMapping("/findById")
    @ApiOperation("findFileById")
    public ApiResponse<String> findFileById(@RequestBody String v){
        try {
            JSONObject obj= JSON.parseObject(v, JSONObject.class);


            ApiResponse<String> result = new ApiResponse(ConfigUtil.selectById(obj.getIntValue("id")));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }
    }

    @GetMapping("/findAll")
    @ApiOperation("findAll")
    public ApiResponse<String> findAll(){
        List<String> all = new ArrayList<>();
        try {

            for(JSONObject obj :ConfigUtil.selectAll()){
                all.add(obj.getString("data"));
            }

            ApiResponse<String> result = new ApiResponse(all);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }
    }


}

